package com.donleo.ssm.controller;

import com.donleo.ssm.dto.UserDto;
import com.donleo.ssm.model.SignModel;
import com.donleo.ssm.model.UserModel;
import com.donleo.ssm.service.ISignService;
import com.donleo.ssm.utils.ResponseLayResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author liangd
 * date 2020-11-27 10:41
 * code 签到前端控制器
 */
@RestController
@RequestMapping("/sign")
public class SignController {

    @Resource
    private ISignService signService;

    /**
     * 查询所有学生(包括组长/助教)
     * @param userDto
     * @return
     */
    @GetMapping("/findAllStudent")
    public ResponseLayResult findAllStudent(UserDto userDto){
        List<UserModel> list = signService.findAllStudent(userDto);
        Integer count = signService.getCount(userDto);
        return ResponseLayResult.createBySuccess(count,list);
    }

    /**
     * 根据用户Id查询当前用户所在小组
     * @param userId 用户ID
     * @return 小组成员
     */
    @GetMapping("/findTeamMemberByUserId")
    public ResponseLayResult findTeamMemberByUserId(Integer userId){
        List<UserModel> list = signService.findTeamMemberByUserId(userId);
        Integer count = signService.getTeamMemberCount(userId);
        return ResponseLayResult.createBySuccess(count,list);
    }

    /**
     * 保存签到数据
     * @param list
     * @return
     */
    @PostMapping("/saveSignData")
    public ResponseLayResult saveSignData(@RequestBody List<SignModel> list){
        Integer count = signService.saveSignData(list);
        return ResponseLayResult.createBySuccess(count,count);
    }

    /**
     * 查询签到记录
     * @param userDto
     * @return
     */
    @GetMapping("/findAllSignRecord")
    public ResponseLayResult findAllSignRecord(UserDto userDto){
        List<UserDto> list = signService.findAllSignRecord(userDto);
        Integer count = signService.getSignRecordCount(userDto);
        return ResponseLayResult.createBySuccess(count,list);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public ResponseLayResult delete(Integer id){
        Integer count = signService.delete(id);
        return ResponseLayResult.createBySuccess(count,count);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/deletes")
    public ResponseLayResult deletes(Integer[] ids){
        List<Integer>idsList = Arrays.asList(ids);
        Integer count = signService.deletes(idsList);
        return ResponseLayResult.createBySuccess(count,count);
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public ResponseLayResult findById(Integer id){
        SignModel signModel = signService.findById(id);
        return ResponseLayResult.createBySuccess(1,signModel);
    }

    /**
     * 修改
     * @param signModel
     * @return
     */
    @PostMapping("/update")
    public ResponseLayResult update(@RequestBody SignModel signModel){
        Integer count = signService.update(signModel);
        return ResponseLayResult.createBySuccess(count,count);
    }
}
